$(function () {
    // 获取卖家产品库
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var result = Model.user.salerProxyGoods();
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    for (var i = 0; i < result.data.length; i++) {
        var item = '<div class="pruduct-manager"><div class="weui-cell"><img class="pruduct-img" src="' + result.data[i].photo + '"><div class="weui-cells"><div class="weui-cell"><h3 class="text-1-6">' + result.data[i].content + '</h3><h3 class="text-1-4">(' + result.data[i].proxyNumber + '人代理)</h3></div><div class="weui-cell"><p class="patron-price">￥' + result.data[i].topPrice + '起</p></div><div class="weui-cell"><p class="text-1-4">剩余' + result.data[i].stock + '个</p><div class="weui-cell__bd pruduct-manager-btn">';
        switch (result.data[i].state) {
            case '下架':
                item += '<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default product-up-btn" data-id="' + result.data[i].goodsId + '">重新上架';
                break;
            case '上架':
                item += '<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default product-down-btn" data-proxy="' + result.data[i].proxyNumber + '" data-id="' + result.data[i].goodsId + '">下架';
                break;
        }
        item += '</a><a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default edit-btn" data-stock="' + result.data[i].stock + '" data-id="' + result.data[i].goodsId + '">编辑库存</a></div></div></div></div></div>';
        $('.product-lists').append(item);
    }
    loading.hide();
});

// 重新上架按钮
$(document).on('click', '.product-up-btn', function () {
    var id = $(this).attr('data-id');
    var confirms = weui.confirm(
        "真的要重新上架吗？",
        function () {
            // 上架产品
            result = Model.user.putOffProxyGoods({
                goodsId: id,
                type: 1
            });
            if (result.code) {
                Common.fn.formatOutput(result);
                return false;
            }
            location.reload();
        },
        function () {}
    );
});

// 下架按钮
$(document).on('click', '.product-down-btn', function () {
    var id = $(this).attr('data-id'),
        proxy = $(this).attr('data-proxy');
    if (proxy > 0) {
        weui.alert('已经有人代言，该产品不能下架', function () {});
        return false;
    }
    var confirms = weui.confirm(
        "真的要下架吗？",
        function () {
            // 上架产品
            result = Model.user.putOffProxyGoods({
                goodsId: id,
                type: 0
            });
            if (result.code) {
                alert(result.message);
                location.reload();
                return false;
            }
            location.reload();
        },
        function () {}
    );
});

